package com.hdc.mumuwei.micro_account_server.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Mumuwei
 * @Description
 * @create 2021/11/30 20:25
 */
public class PassUtils {

    /**
     * MD5加密
     */
    public static String EncoderByMd5(String str) {
        MessageDigest md5 = null;
        try {
            //创建MD5核心对象
            md5 = MessageDigest.getInstance("MD5");
            // 将字符编码改为UTF-8
            md5.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte b[] = md5.digest();
        int i;
        StringBuffer buf = new StringBuffer();
        for (int offset = 0; offset < b.length; offset++) {
            //每次循环的时候需要从digest数组中随机取出2个不同的字符
            i = b[offset];
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(i));
        }
        return buf.toString();
    }

    public static String createMD5(String pass){
        String password = EncoderByMd5(EncoderByMd5(EncoderByMd5(pass)+"mumuwei")+"mumuwei");
        return password;
    }
}